#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

// 求最长递增子序列的长度
int lengthOfLIS(vector<int>& nums) {
    vector<int> tails;
    for (int num : nums) {
        auto it = lower_bound(tails.begin(), tails.end(), num);
        if (it == tails.end()) {
            tails.push_back(num);
        } else {
            *it = num;
        }
    }
    return tails.size();
}

// 求最长公共子序列的长度
int longestCommonSubsequence(int n, int a[], int b[]) {
    unordered_map<int, int> pos;
    // 记录 b 中每个元素的位置
    for (int i = 1; i <= n; i++) {
        pos[b[i]] = i;
    }
    vector<int> newSeq;
    // 构建新的位置序列
    for (int i = 1; i <= n; i++) {
        if (pos.count(a[i])) {
            newSeq.push_back(pos[a[i]]);
        }
    }
    // 求新序列的最长递增子序列的长度
    return lengthOfLIS(newSeq);
}

int main() {
    int n;
    cin >> n;
    int a[100007];
    int b[100007];
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> b[i];
    // 调用函数计算最长公共子序列的长度
    int result = longestCommonSubsequence(n, a, b);
    cout << result << endl;
    return 0;
}